package cn.bjc.org.service.impl;

import cn.bjc.basic.exception.BusinessException;
import cn.bjc.basic.service.impl.BaseServiceImpl;
import cn.bjc.basic.util.BaiduAuditUtils;
import cn.bjc.org.domain.Employee;
import cn.bjc.org.domain.Shop;
import cn.bjc.org.domain.ShopLog;
import cn.bjc.org.dto.ShopDto;
import cn.bjc.org.mapper.EmployeeMapper;
import cn.bjc.org.mapper.ShopLogMapper;
import cn.bjc.org.mapper.ShopMapper;
import cn.bjc.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
//将当前类自动注入到spring容器中
//声明将这个类有事务管理,父类有事务，该注解有继承性
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    
    @Autowired
    private ShopMapper shopMapper;
    
    @Autowired
    private EmployeeMapper employeeMapper;
    
    @Autowired
    private ShopLogMapper LogMapper;

    @Autowired
    private JavaMailSender javaMailSender;
    
    @Override
    public void settlement(Shop shop) {
        //实现shop入驻流程
        
        //1.非空校验
        if(StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(shop.getTel())
                
                || StringUtils.isEmpty(shop.getAdmin().getUsername())
                || StringUtils.isEmpty(shop.getAdmin().getPhone())
                || StringUtils.isEmpty(shop.getAdmin().getEmail())
                || StringUtils.isEmpty(shop.getAdmin().getPassword())
                || StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())
        ){
            //提示店铺商家,参数不能为空
            throw new BusinessException("参数不能为空");
        }
        
        //通过百度AI审查商店名称是否合法
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if(!aBoolean){//名称有问题
            throw new BusinessException("店铺名称违规");
        }

        Employee admin = shop.getAdmin();
                
        //2.两次密码不一致校验(前端校验了,这里也要校验,这叫前后端双重校验
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致");
        }
        
        //3.店铺重复(是否注册过校验)
        Shop dbShop = shopMapper.getShopByMessage(shop);
        if(dbShop!=null){
            throw new BusinessException("店铺已经被注册过");
        }
        
        //4.把shop和employee数据保存在表中(先生成employee,再生成shop)
        //1.保存employee获取自增的主键
        employeeMapper.save(admin);
        
        //2.把自增的主键设置到shop表中的admin_id中
        shop.setAdmin_id(admin.getId());
        
        //3.保存shop数据,返回自增的主键
        shopMapper.save(shop);
        
        //4.返回的自增的主键设置给employee的shop_id字段
        admin.setShop_id(shop.getId());
        
        //5.更新employee数据
        employeeMapper.update(admin);
        
    }

    //审核通过
    @Override
    public void pass(ShopLog log) throws MessagingException { //log:  shop_id note
        //1.修改shop状态
        //通过shop_id修改状态
        Shop shop = shopMapper.loadById(log.getShop_id());
        //审核驳回
        shop.setState(2);
        shopMapper.update(shop);
        
        //2.录入日志
        log.setState(2);
        log.setAudit_id(1L);
        LogMapper.save(log);
        //3.通知商家 邮件发送
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1912813725@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h2>你的店铺入驻申请通过!!!</h2><a href='http://localhost:8080/shop/active/\"+log.getShop_id()+\"'>点击该链接激活</a>",true);
        
        //获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("1912813725@qq.com");
        //发送邮件
        javaMailSender.send(mimeMessage);
        
    }
    
    //审核驳回
    @Override
    public void reject(ShopLog log) throws MessagingException{
        //1.修改shop状态
        //通过shop_id修改状态
        Shop shop = shopMapper.loadById(log.getShop_id());
        //审核通过
        shop.setState(4);
        shopMapper.update(shop);

        //2.录入日志
        log.setState(4);
        log.setAudit_id(1L);
        LogMapper.save(log);
        //3.通知商家 邮件发送
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1912813725@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h2>你的店铺已经注册!!!</h2>");

        //获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("1912813725@qq.com");
        //发送邮件
        javaMailSender.send(mimeMessage);


        /**
         * 两种状态
         * 1.修改状态为 state=4  下次入驻的时候判断该店铺是否是被驳回过,
         * 2.直接删除shop,emp
         * 
         */
    }

    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }
}
